package com.yanqu.road.server.cmd.wingroom;

import com.yanqu.road.entity.wingroom.UserFurnitureSample;
import com.yanqu.road.pb.wingroom.WingRoomProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.wingroom.WingRoomChakraModule;
import com.yanqu.road.server.gameplayer.module.wingroom.WingRoomModule;
import com.yanqu.road.server.pb.WingRoomPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

/**
 * @author ：zfr
 * @date ：2022/2/8 16:31
 */
@Cmd(code = GameProtocol.S_WING_ROOM_FURNITURE_SAMPLE_LV_UP, desc = "家具图鉴升级")
public class FurnitureSampleLvUpCmd extends AbstractCommand {
    private int furnitureId;
    private boolean oneKey;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        WingRoomProto.FurnitureSampleLvUpReqMsg reqMsg = WingRoomProto.FurnitureSampleLvUpReqMsg.parseFrom(packet.getBytes());
        furnitureId = reqMsg.getFurnitureId();
        oneKey = reqMsg.hasOneKey() && reqMsg.getOneKey();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (oneKey) {
            ret = player.getModule(WingRoomModule.class).furnitureSampleLvUpOneKey();
        } else {
            ret = player.getModule(WingRoomModule.class).furnitureSampleLvUp(furnitureId);
        }
        if (ret == 0) {
            //重新计算厢房风水上限
            long windWaterLimit = player.getModule(WingRoomChakraModule.class).countWindWaterLimit();
            player.getModule(WingRoomChakraModule.class).getUserWingRoomChakra().setWindWaterLimit(windWaterLimit);
            //计算风水数据，并同步
            player.getModule(WingRoomChakraModule.class).recountWindWaterData();
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if (!oneKey) {
            WingRoomProto.FurnitureSampleLvUpRespMsg.Builder respMsg = WingRoomProto.FurnitureSampleLvUpRespMsg.newBuilder();
            respMsg.setRet(ret);
            if (ret == 0) {
//                //解锁/升级家具，风水上限提升
//                player.getModule(WingRoomChakraModule.class).addWindWaterLimit(furnitureId);
                //解锁/升级家具，奖励
                UserFurnitureSample furnitureSample = player.getModule(WingRoomModule.class).getFurnitureSample(furnitureId);
                //升级奖励领取
                Property reward = player.getModule(WingRoomChakraModule.class).upgradeFurnitureRewardGet(furnitureSample.getFurnitureId(), furnitureSample.getLv());
                //pb
                respMsg.setSample(WingRoomPb.buildFurnitureSampleTempMsg(furnitureSample));
                respMsg.setReward(PropertyHelper.parsePropertyToString(reward));
            }
            player.sendPacket(Protocol.U_WING_ROOM_FURNITURE_SAMPLE_LV_UP, respMsg);
        }
    }
}
